package modele;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;


import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;


public class ModeleSauvegarde extends ModeleXml 
{

	private Element classes;
	private Element relations;
	private Vector<ModeleClasse> listeClasse;
	private Vector<ModeleRelation> listeRelation;
	
	public ModeleSauvegarde(Vector<ModeleClasse> vc, Vector<ModeleRelation> mr) 
	{
		super.root = new Element("uml");
		super.doc = new Document(super.root);
		
		this.classes = new Element("classes");
		this.relations = new Element("relations");
		this.root.addContent(this.classes);
		this.root.addContent(this.relations);
		
		this.listeClasse = vc;
		this.listeRelation = mr;
	}
	
	
	public List<Element> cloneListe(ModeleXml mx)
	{
			//On copie l'arborescence
			List<Element> listCopie = mx.getDoc().cloneContent();
			return listCopie;
	}
	
	public void sauvegarder(String urlFichier) throws IOException
	{
		
			File f = new File(urlFichier);
			XMLOutputter op = new XMLOutputter(Format.getPrettyFormat());
			FileOutputStream ou = new FileOutputStream(f);
			
			//on ecrit la partie concernant les classes
			for(int i = 0 ; i < listeClasse.size();i++)
			{
				this.classes.addContent(cloneListe((ModeleClasse)listeClasse.get(i)));		
			}
			
			//et celle concernant les relations
			for(int i = 0 ; i < listeRelation.size();i++)
			{
				this.relations.addContent(cloneListe((ModeleRelation)listeRelation.get(i)));		
			}
			//on "publie" le tout
			op.output(this.doc,ou);
		}
	
}